package main

import (
	"fmt"
	"path/filepath"
)
import "math"
import "errors"

func main() {

	// 将分割符转为/
	fmt.Println(filepath.ToSlash(`C:\python37\python.exe`))

	mapTest()

	// r := Rect {2.0,5}
	// fmt.Println("r: ",r)

	// are := r.area()
	// fmt.Println("area: ",are)

	// circle := Circle{radius:10}
	// circleArea := circle.area()
	// perim := circle.perim()
	// fmt.Println("circleArea: ",circleArea)
	// fmt.Println("circlePerim: ",perim)

	//这样写不行的
	// geo := measure(r).perim()
	// fmt.Println("geo: ",geo)

	// measure(circle)

	/*
			for _, i := range []int{42, 50} {
				//注意在if行内的错误检查代码，在go中是一个普遍的用法
		        if r, e := handleErrorFunc(i); e != nil {
		            fmt.Println("failed:", e)
		        } else {
		            fmt.Println("worked:", r)
		        }
		    }
	*/

	/*
			for _, i := range []int{7,42} {
		        if r, e := myError(i); e != nil {
		            fmt.Println("myError failed:", e)
		        } else {
		            fmt.Println("myError worked:", r)
		        }
		    }
	*/

	/*
		errStruct := argError{100, "success"}
		myErrorStr := errStruct.Error()
		fmt.Println("myError函数内容为：", myErrorStr)
	*/
}

func mapTest() {
	//对map操作
	cityCodes := map[string]interface{}{
		"北京": 1,
		"上海": 2,
		"南京": 3,
		"广州": 4,
		"深圳": 5,
	}

	loveCityCodes := map[string]interface{}{
		"合肥": 6,
		"苏州": 7,
		"无锡": 8,
		"常州": 9,
		"芜湖": 10,
	}
	MapJoin(&cityCodes, &loveCityCodes)
	fmt.Println("cityCodes111:", cityCodes)
	fmt.Println("cityCodes222:", loveCityCodes)

}

func MapJoin(m *map[string]interface{}, m2 *map[string]interface{}) {
	if m != nil && m2 != nil {
		for k, v := range *m2 {
			(*m)[k] = v
		}
	}
}

// 错误处理
func handleErrorFunc(arg int) (int, error) {
	if arg == 42 {
		//errors.New构造一个使用给定的错误信息的基本error值。
		return -1, errors.New("can't work with 42")
	}
	return arg + 100, nil
}

func myError(arg int) (int, error) {
	if arg == 42 {
		//errors.New构造一个使用给定的错误信息的基本error值。
		return -1, &argError{arg, "can't work with it"}
	}
	return arg + 10, nil
}

type argError struct {
	arg  int
	prob string
}

func (error *argError) Error() string {
	str := fmt.Sprintf("值arg: %d    错误：%s", error.arg, error.prob)
	return str
}

type Geometry interface {
	area() float64
	perim() float64
}

func measure(g Geometry) {
	// fmt.Println(g)
	fmt.Println(g.area())
	// fmt.Println(g.perim())
	// return g
}

type Rect struct {
	width, height float64
}

type Circle struct {
	radius float64
}

/*
func (r Rect) area() float64 {
	area := r.width * r.height
	return area
}

func (r Rect) perim() float64 {
	perim := 2 * (r.width + r.height)
	return perim
}
*/

func (circle Circle) area() float64 {
	area := math.Pi * circle.radius * circle.radius
	return area
}

func (circle Circle) perim() float64 {
	perim := 2 * math.Pi * circle.radius
	return perim
}
